package com.zzxx.wechat.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzxx.wechat.common.enums.UserAttentionTypeEnum;
import com.zzxx.wechat.dao.ArcteryxSkuDao;
import com.zzxx.wechat.dao.UserAttentionDao;
import com.zzxx.wechat.entity.ArcteryxSku;
import com.zzxx.wechat.entity.UserAttention;
import com.zzxx.wechat.util.URLValidator;
import com.zzxx.wechat.util.WechatConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CommandService {


    @Autowired
    UserAttentionDao userAttentionDao;
    @Autowired
    ArcteryxSkuDao arcteryxSkuDao;

    public String addCommand(String openId, String url) {
        if (!URLValidator.isValid(url)) {
            return "添加指令非链接，请重新输入";
        }

        List<ArcteryxSku> arcteryxSkuList = arcteryxSkuDao.list();

        ArcteryxSku firstAku = arcteryxSkuList.get(0);
        UserAttention userAttention = userAttentionDao.queryByOpenIdAndSpuId(openId, firstAku.getSpuId());
        if (userAttention == null) {
            userAttention = new UserAttention();
            userAttention.setOpenId(openId);
            userAttention.setSpuId(firstAku.getSpuId());
            userAttention.setSpuUrl(url);
            userAttention.setType(UserAttentionTypeEnum.ARCTERY.getCode());
            userAttentionDao.save(userAttention);

        }
        StringBuilder message = new StringBuilder(WechatConstant.SEPARATOR + WechatConstant.HEADER + WechatConstant.SEPARATOR);
        for (ArcteryxSku arcteryxSku : arcteryxSkuList) {
            message.append(String.format(WechatConstant.CONTENT, arcteryxSku.getSkuName(), arcteryxSku.getSizeLabel(), arcteryxSku.getInventory(), arcteryxSku.getDiscountPrice()));
        }
        message.append("详情: https:www.baudu.com");
        return message.toString();
    }


    public String calCommand(String openId, String url) {
        userAttentionDao.update(Wrappers.<UserAttention>lambdaUpdate().eq(UserAttention::getOpenId, openId).eq(UserAttention::getSpuUrl, url).set(UserAttention::getIsDel, Boolean.FALSE));

        return "删除成功";
    }

    public String queryAttention(String openId) {

        return "";
    }

}
